<template>
  <div class="body">
    <div class="table_content">
      <div class="table_filtrate">
        <div v-if="jsAuthJudgment('manage')" class="tf_item">
          <div class="tag">商务</div>
          <el-select
            v-model="tableParams.business_uid"
            class="input"
            placeholder="请选择"
            size="mini"
          >
            <el-option label="全部" :value="0" />
            <el-option
              v-for="item in tableFiltrateData.business_list"
              :key="item.id"
              :label="item.nickname"
              :value="item.id"
            >
              <span style="float: left">{{ item.nickname }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.group_name
              }}</span>
            </el-option>
          </el-select>
        </div>

        <div class="tf_item" style="margin-right: 30px">
          <div class="tag">操作方式</div>
          <el-select
            placeholder="操作方式"
            clearable
            size="mini"
            v-model="tableParams.target_msg"
            :filterable="true"
            class="inp"
            no-data-text="请先选择"
          >
            <el-option label="添加机构" value="添加机构"> </el-option>
            <el-option label="修改机构" value="修改机构"> </el-option>
            <el-option label="删除机构" value="删除机构"> </el-option>
            <el-option label="机构审核批复" value="机构审核批复"> </el-option>
            <el-option label="提审机构账单" value="提审机构账单"> </el-option>


            <el-option label="添加产品" value="添加产品"> </el-option>
            <el-option label="修改产品" value="修改产品"> </el-option>
            <el-option label="删除产品" value="删除产品"> </el-option>


            <el-option label="添加渠道" value="添加渠道"> </el-option>
            <el-option label="修改渠道" value="修改渠道"> </el-option>


            
            <el-option label="添加广告" value="添加广告"> </el-option>
            <el-option label="修改广告" value="修改广告"> </el-option>
            <el-option label="广告退款" value="广告退款"> </el-option>
            <el-option label="广告充值" value="广告充值"> </el-option>


            <el-option label="产品线索更新" value="产品线索更新"> </el-option>
            <el-option label="查看手机号" value="查看手机号"> </el-option>

            <el-option label="更新用户" value="更新用户"> </el-option>
            <el-option label="修改黑名单" value="修改黑名单"> </el-option>


          </el-select>
        </div>


        <div class="tf_item">
          <el-button @click="loadTableData" type="primary" plain size="mini"
            >搜索</el-button
          >
        </div>
      </div>

      <div class="table-card">
        <el-table
          class="table"
          :data="tableData.data"
          :row-class-name="tableRowClassName"
          v-loading="loading.table"
          :border="true"
          size="mini"
        >
          <el-table-column prop="id" label="ID" width="120"> </el-table-column>
          <el-table-column label="操作人" width="120">
            <template slot-scope="scope">
              {{ scope.row.operator_user.nickname }}
            </template>
          </el-table-column>
          <el-table-column prop="target_msg" label="事件" width="160">
          </el-table-column>

          <el-table-column prop="action_msg" label="操作内容">
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleTableSizeChange"
          @current-change="handleTableCurrentChange"
          :current-page="currentPage4"
          :page-sizes="$pageSize"
          :page-size="tableParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiAgentList, apiManageLogs } from "@/api/apiCommon";
import { paramsDeleteNull } from "@/utils/common";
import { authJudgment, App_getSummaries } from "@/utils/appUtil";
export default {
  name: "Index",
  data() {
    return {
      tableFiltrateData: {
        business_list: [],
      },
      tableParams: {
        page: 1,
        size: 15,
        business_uid: 0,
        target_msg: "",
        time_interval: [
          new Date(new Date().setHours(0, 0, 0, 0)), // 今天的初始时间戳
          new Date(new Date().setHours(23, 59, 59, 999)), // 今天的结束时间戳
        ],
      },
      tableData: {
        current_page: 1,
        data: [],
        last_page: 1,
        per_page: "15",
        total: 0,
      },
      currentPage4: 1,
      business_select: [],
      organization_select: [],
      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      loading: {
        dialog_create: false,
        table: false,
        form: false,
      },
    };
  },
  created() {
    this.loadTableData();
    this.req_agent();
  },
  methods: {
    // 权限判断
    jsAuthJudgment(agree_group) {
      return authJudgment(agree_group);
    },

    // 获取商务人员
    req_agent() {
      const _this = this;
      apiAgentList({
        page: 1,
        size: 99,
        status: "normal",
        group_id: "2,3,5",
      })
        .then((response) => {
          const result = response.data.datalist.data;
          _this.tableFiltrateData.business_list = result;
        })
        .catch(function (result) {
          _this.$message.error(result.msg);
        });
    },
    // 列表请求
    loadTableData() {
      const _this = this;
      _this.loading_start("table");
      var tableParamsData = { ..._this.tableParams };
      tableParamsData = paramsDeleteNull(tableParamsData, "business_uid");
      tableParamsData = paramsDeleteNull(tableParamsData, "o_id");
      tableParamsData = paramsDeleteNull(tableParamsData, "fuzzy_search");
      tableParamsData = paramsDeleteNull(tableParamsData, "loan_type");
      tableParamsData = paramsDeleteNull(tableParamsData, "push_status");
      tableParamsData = paramsDeleteNull(tableParamsData, "entry_status");
      // var start_date = tableParamsData["time_interval"][0];
      // var end_date = tableParamsData["time_interval"][1];
      // tableParamsData["start_time"] = dateToTimestamp(start_date);
      // tableParamsData["end_time"] = dateToTimestamp(end_date);
      // delete tableParamsData["time_interval"];

      apiManageLogs(tableParamsData)
        .then((response) => {
          const result = response;
          _this.tableData = result.data.datalist;
          // var datalist = result.data.datalist;
          // var data = datalist['data'];
          // console.log(data,'data')
          // _this.tableDataProcess(result.data.datalist.data);
          _this.loading_end("table");
        })
        .catch(function (result) {
          _this.$message.error(result.msg);
          _this.tableData.data = null;
          _this.loading_end("table");
        });
    },
    /** 更新数据加工 */
    tableDataProcess(data) {
      // console.log(data, "data");
      data = data[0];
      Object.entries(data).forEach(([key, value]) => {
        // console.log([key, value]);
      });
    },

    // 页面跳转
    handleToGoCreate() {
      this.$router.push({ path: "/product/create", query: { id: 0 } });
    },
    // 表格行状态
    tableRowClassName({ row, rowIndex }) {
      if (row["status"] == "stop") {
        return "discard-row";
      }
    },
    // 表格操作
    handleTableEdit(index, row) {
      this.$router.push({ path: "/product/update", query: { id: row.id } });
    },
    // 表格恢复
    handleTableRegain(index, row) {
      var _this = this;
      var params = {
        id: row.id,
        status: "normal",
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 表格删除
    handleTableDelete(index, row) {
      const _this = this;
      _this.loading_start("table");
      apiProductDelete({
        id: row.id,
      }).then((response) => {
        this.$message({
          message: response.msg,
          type: "success",
        });
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 数量改变
    handleTableSizeChange(val) {
      this.tableParams.size = val;
      this.loadTableData();
    },
    // 页数改变
    handleTableCurrentChange(val) {
      this.tableParams.page = val;
      this.loadTableData();
      // console.log(`当前页: ${val}`);
    },
    // 部分状态改变
    tableUpdateItem(item) {
      var _this = this;
      var params = {
        id: item.id,
        push_status: item.push_status,
        entry_status: item.entry_status,
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
      });
    },
    // 加载开始...
    loading_start(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = true;
          setTimeout(() => {
            this.loading[key] = false;
          }, 5000);
        });
      } else {
        this.loading[keys] = true;
        setTimeout(() => {
          this.loading[keys] = false;
        }, 5000);
      }
    },
    // 加载结束...
    loading_end(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = false;
        });
      } else {
        this.loading[keys] = false;
      }
    },
  },
};
</script>

<style lang="scss"></style>
